import request from '../http/request';


// 获取验证码
export function getCaptcha() {
    return request({
      url: '/captcha/arithmetic',
      method: 'get',
      responseType: 'arraybuffer'  // 确保以二进制流的形式接收
    }).then(response => {
      // 将 ArrayBuffer 转换为 Base64 字符串
      const base64 = arrayBufferToBase64(response.data);
      return `data:image/gif;base64,${base64}`;
    });
  }
  
// 验证验证码
export function validateCaptcha(data) {
  return request({
    url: '/verify/code',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  }).then(response => response.data);
}

// 登录方法
export function login(data) {
  return request({
    url: '/verify/login', // 登录接口
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data
  }).then(response => response.data);
}



  // ArrayBuffer 转 Base64 的工具函数
  function arrayBufferToBase64(buffer) {
    let binary = '';
    const bytes = new Uint8Array(buffer);
    const len = bytes.byteLength;
    for (let i = 0; i < len; i++) {
      binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
  }